package cn.hls.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.alibaba.druid.stat.TableStat.Condition;

import cn.hls.dao.ShipperDao;
import cn.hls.entity.Shipper;
import cn.hls.entity.vo.PageInfo;
import cn.hls.exception.BussinessException;
import cn.hlsconstant.DataAssessEnum;
/**
  *@fileName: ShipperDaoImpl.java
  *@package: cn.hls.dao.impl
　*@Description:　托运方数据操作接口实现类
　*@author: yinghui.zhang
　*@email: uuhui@163.com
  *@date: 2016年6月10日
 *
 */
@Component("shipperDao")
public class ShipperDaoImpl extends BaseDaoImpl<Shipper> implements ShipperDao {
	@Override
	public PageInfo<Shipper> loadShipperInfo(Shipper entity, PageInfo<Shipper> page) throws Exception {
		// 定义查询Hql语句
		StringBuilder selectHql=new StringBuilder("from Shipper p inner join fetch p.company c　 ");
		// 定义汇总Hql语句
		StringBuilder counthql = new StringBuilder("select count(p.id) from Shipper p inner join  p.company c ");
		//定义查询条件hql
		StringBuilder condHql=new StringBuilder(" where p.deleted=? ");
		// 定义查询参数
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(false);
		if (entity != null) {
			//按企业查询
			if(entity.getCompany()!=null){
				if(StringUtils.hasText(entity.getCompany().getName())){
					condHql.append(" and c.name like ? " );
					paramList.add("%"+entity.getCompany().getName().trim()+"%");
				}
				//按编号查询
				if(entity.getCompany().getId()!=null){
					condHql.append("and c.id=? ");
					paramList.add(entity.getCompany().getId());
				}
				//按搬运企业的承运企业编号查询
				if(entity.getCompany().getCarryCompanyId()!=null){
					condHql.append("and p.company.carryCompanyId=? ");
					paramList.add(entity.getCompany().getCarryCompanyId());
				}
			}
			//按开户银行查询
			if(StringUtils.hasText(entity.getBankAccount())){
				condHql.append(" and p.bankAccount like ? ");
				paramList.add("%"+entity.getBankAccount().trim()+"%");
			}
			//按状态查询
			if(StringUtils.hasText(entity.getStatus())){
				condHql.append("and p.status=? ");
				paramList.add(entity.getStatus());
			}
		}
		counthql. append(condHql);
		selectHql.append(condHql);
		selectHql.append(" order by p.id desc ");
		return find(selectHql.toString(), counthql.toString(), paramList.toArray(), page);
	}
	
	@Override
	public List<Shipper> loadShipperList(Shipper entity)throws Exception{
		// 定义查询Hql语句
		StringBuilder selectHql=new StringBuilder("from Shipper p where p.deleted=? ");
		// 定义查询参数
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(false);
		if (entity != null) {
			//按企业查询
			if(entity.getCompany()!=null){
				if(StringUtils.hasText(entity.getCompany().getName())){
					selectHql.append(" and c.name like ? " );
					paramList.add("%"+entity.getCompany().getName().trim()+"%");
				}
			}
			//按开户银行查询
			if(StringUtils.hasText(entity.getBankAccount())){
				selectHql.append(" and p.bankAccount like ? ");
				paramList.add("%"+entity.getBankAccount().trim()+"%");
			}
			//按状态查询
			if(StringUtils.hasText(entity.getStatus())){
				selectHql.append("and p.status=? ");
				paramList.add(entity.getStatus());
			}
			 
		}
		return findByHql(selectHql.toString(), paramList.toArray());
	}

	@Override
	public void delete(Object entityId) throws Exception {
		String []ids=(String[])entityId;
		StringBuilder hql=new StringBuilder("update Shipper set deleted=? where id in (");
		List<Object>params=new ArrayList<Object>();
		params.add(true);
		for (String id:ids){
			hql.append("?,");
			params.add(Long.valueOf(id));
		}	
		hql.delete(hql.length()-1, hql.length());
		hql.append(")");
		 try {
			executeHQLUpdate(hql.toString(), params.toArray());
		} catch (Exception e) {
			throw new BussinessException(DataAssessEnum.UPDATE_ERROR,"删除托运方信息失败" );
		}
	}

	
	@Override
		public boolean exists(Shipper entity)throws Exception{
			StringBuilder selectHql=new StringBuilder("select count(p.id) from Shipper p where p.deleted=? ");
			List<Object>params=new ArrayList<Object>();
			params.add(false);
			if(entity!=null){
				if(entity.getCompany()!=null){
					if(entity.getCompany().getId()!=null){
						selectHql.append("and p.company.id=? ");
						params.add(entity.getCompany().getId());
					}
				}
			if(StringUtils.hasText(entity.getContractor())){
					selectHql.append(" and p.contractor=? ");
					params.add(entity.getContractor().trim());
				}
				if(StringUtils.hasText(entity.getAddress())){
					selectHql.append("and p.address=? ");
					params.add(entity.getAddress().trim());
				}
			}
			Query query=this.getSession().createQuery(selectHql.toString());
			for(int i=0;i<params.size();i++){
				query.setParameter(i, params.get(i));
			}
			Long c = Long.valueOf(((Number) query.uniqueResult()).longValue());
			return c.intValue()>0?true:false;
	}
	
}
